package cn.maihe.elg.operation.centers.hymake.channel.service;

import cn.maihe.elg.operation.centers.hymake.channel.HyMakeBaseChannelService;
import cn.maihe.elg.operation.centers.hymake.dto.channel.BondDownLoadReqDTO;
import cn.maihe.elg.operation.centers.hymake.dto.channel.ChlBizRespData;
import cn.maihe.elg.operation.centers.hymake.dto.channel.vo.HyMakeinvoiceObjVO;
import cn.maihe.elg.operation.centers.hymake.enums.HyMakeChannelMethod;
import cn.maihe.elg.operation.centers.hymake.enums.HyMakeRespCodeEnum;
import cn.maihe.elg.operation.centers.supports.model.bo.PushCenterGuaranteeBo;
import cn.maihe.elg.operation.model.dto.InnerBaseResp;
import cn.maihe.elg.operation.model.enums.AttachmentFileType;
import cn.maihe.elg.operation.model.enums.CenterNoEnum;
import cn.maihe.elg.operation.repository.entity.AcceptOrderInfoDO;
import cn.maihe.elg.operation.repository.entity.GuaranteeVo;
import cn.maihe.elg.operation.repository.entity.InvoiceInfoDO;
import cn.maihe.elg.operation.service.business.ExchangeCenterService;
import cn.maihe.elg.operation.service.invoice.InvoiceInfoService;
import cn.maihe.elg.operation.service.order.AcceptOrderInfoService;
import cn.maihe.elg.operation.service.order.GuaranteeAttachmentInfoService;
import cn.maihe.elg.operation.supports.system.ElgResourceService;
import cn.maihe.elg.operation.utils.DateUtil;
import cn.maihe.elg.operation.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;

/**
 * @Description 出函机构出函后通知 电子保函至平台侧
 * 出函机构（保险公司/银行）在出函后向电子保函平台推送电子保函的下载地址
 * @Author WJH
 * @Date 2021/04/21
 */
@Service
@Slf4j
public class HyMakeBondDownloadChannelService extends HyMakeBaseChannelService<BondDownLoadReqDTO, ChlBizRespData> {

    @Resource
    private ElgResourceService elgResourceService;
    @Resource
    private AcceptOrderInfoService acceptOrderInfoService;
    @Resource
    private GuaranteeAttachmentInfoService guaranteeAttachmentInfoService;
    @Resource
    private InvoiceInfoService invoiceInfoService;
    @Resource
    private ExchangeCenterService exchangeCenterService;

    @Override
    protected HyMakeChannelMethod methodEnum() {
        return HyMakeChannelMethod.bondDownLoad;
    }

    public InnerBaseResp<String> bondDownload(PushCenterGuaranteeBo bo) {
        GuaranteeVo vo = bo.getGuaranteeVo();
        String applyStatus = bo.getApplyStatus();
        String acceptOrderNo = vo.getAcceptOrderNo();

        AcceptOrderInfoDO acceptOrderInfo = acceptOrderInfoService.getByAcceptOrderNo(vo.getAcceptOrderNo());
        Assert.notNull(acceptOrderInfo, "查无此受理订单记录");

        CenterNoEnum curCenter = exchangeCenterService.getCenterNoEnumById(acceptOrderInfo.getCenterId());

        BondDownLoadReqDTO reqDTO = new BondDownLoadReqDTO();
        reqDTO.setInsuranceNo(acceptOrderInfo.getAcceptOrderNo());
        reqDTO.setPolicyNo(acceptOrderInfo.getGuaranteeNo());
        String policyUrl = guaranteeAttachmentInfoService.buildAttachDownloadUrl(acceptOrderNo, AttachmentFileType.TYPE_7);
        reqDTO.setGuaranteeUrl(policyUrl);
        // 申请书+协议书 合同
        reqDTO.setPolicyUrl(policyUrl);
        reqDTO.setGuaranteeOutTime(DateUtil.formateDate(vo.getGuaranteeTime(), "yyyy-MM-dd HH:mm:ss"));
        reqDTO.setInsurStartDate(DateUtil.formateDate(vo.getServiceBeginTime(), "yyyy-MM-dd"));
        reqDTO.setInsurEndDate(DateUtil.formateDate(vo.getServiceEndTime(), "yyyy-MM-dd"));
        /**
         * 担保责任：一般保证、连带责任保证、独立保函
         */
        reqDTO.setGuaranteeDuty("独立保函");
        /**从属性：独立保函、从合同*/
        reqDTO.setGuaranteeProperty("独立保函");
        reqDTO.setClaimClause(this.getClaimClause());
        if (CenterNoEnum.NINGDE.equals(curCenter)) {
            reqDTO.setPayLimitation("10日内");
        } else {
            reqDTO.setPayLimitation("7日内");
        }
        reqDTO.setBankRecordNo("无");
        reqDTO.setApplicableLaw("担保法、保险法、《独立保函司法解释》（法释〔2016〕24 号）");
        reqDTO.setRemark("担保公司保函");
        reqDTO.setReceiptUrl(guaranteeAttachmentInfoService.buildAttachDownloadUrl(acceptOrderNo, AttachmentFileType.TYPE_6));
        reqDTO.setGuaranteeOfdUrl(guaranteeAttachmentInfoService.buildAttachDownloadUrl(acceptOrderNo, AttachmentFileType.TYPE_0));
        reqDTO.setGuaranteeOfdMD5(guaranteeAttachmentInfoService.getTypeAttachmentByAcceptOrderNo(acceptOrderNo, AttachmentFileType.TYPE_0).getFileMd5());
        reqDTO.setGuaranteeXmlUrl(guaranteeAttachmentInfoService.buildAttachDownloadUrl(acceptOrderNo, AttachmentFileType.TYPE_14));
        reqDTO.setGuaranteeXMlMD5(guaranteeAttachmentInfoService.getTypeAttachmentByAcceptOrderNo(acceptOrderNo, AttachmentFileType.TYPE_14).getFileMd5());
        reqDTO.setInvoiceObj(this.buildInvoiceObj(acceptOrderInfo));

        InnerBaseResp<String> innerBaseResp = null;


        CenterNoEnum centerNoEnum = this.getCenterNoEnumById(acceptOrderInfo.getCenterId());
        try {
            ChlBizRespData bizRespData = this.doRequest(centerNoEnum, reqDTO);
            if (HyMakeRespCodeEnum.isSuccess(bizRespData.getCode())) {
                innerBaseResp = InnerBaseResp.success(bo);
            } else {
                innerBaseResp = InnerBaseResp.error(bo, bizRespData.getMessage());
            }
        } catch (Exception e) {
            log.error("开函结果通知 >> [{}] 异常:{}", centerNoEnum.getCenterName(), e.getMessage(), e);
            innerBaseResp = InnerBaseResp.error("通知异常:" + e.getMessage());
        }

        return innerBaseResp;
    }

    private HyMakeinvoiceObjVO buildInvoiceObj(AcceptOrderInfoDO acceptOrderInfo) {
        InvoiceInfoDO info = invoiceInfoService.selectById(acceptOrderInfo.getInvoiceId());

        HyMakeinvoiceObjVO objVO = new HyMakeinvoiceObjVO();
        objVO.setInvoiceUrl(info.getPdfUrl());
        objVO.setInvoiceCode(info.getInvoiceCode());
        objVO.setInvoiceNo(info.getInvoiceNo());
        objVO.setApplyStatus(StringUtil.isNotBlank(info.getInvoiceNo()) ? "1" : "0");
        objVO.setOpenTime(DateUtil.formateDate(info.getGmtCreate(), "yyyy-MM-dd HH:mm:ss"));
        objVO.setInvoiceTitle(info.getInvoiceTitle());
        objVO.setCustomerAddress(info.getCustomerAddress());
        objVO.setCustomerPhone(info.getCustomerPhone());
        objVO.setProductName("*金融服务*保函费");
        objVO.setAmount(StringUtil.toYuan(info.getAmount()));
        objVO.setAmountOfChinaese(info.getAmountOfChinaese());
        objVO.setSellerName(info.getSellerName());
        objVO.setSellerCreditCode(info.getSellerCreditCode());
        objVO.setSellerBankCode(info.getSellerBankCode());
        objVO.setSellerOpenBank(info.getSellerOpenBank());
        objVO.setSellerAddress(info.getSellerAddress());
        objVO.setSellerPhone(info.getSellerPhone());
        objVO.setHandler(info.getHandler());
        objVO.setInvoiceRemark(info.getInvoiceRemark());
        return objVO;
    }


    /**
     * 索赔条款
     *
     * @return
     */
    public String getClaimClause() {
        return "1、投标人在投标有效期内撤销或修改其投标文件的；\n" +
                "2、投标人中标后，非因不可抗力原因放弃中标、无正当理由不与招标人订立合同、在签订合同时向招标人提出附加条件、或者不按照招标文件要求提交履约担保金的；\n" +
                "3、投标人的投标文件存在投标须知第20.6款规定的雷同情形之一；\n" +
                "4、投标人中标后，因违法行为导致中标被依法确认无效的；\n" +
                "5、法律、法规规定的其他没收投标保证金情形。\n" +
                "本保函在投标有效期到期后28日（含）内或招标人延长投标有效期后的到期日后28日（含）内保持有效，延长投标有效期无须通知我方，但任何索款要求应在投标有效期内送达我方。保函失效后请将本保函交投标人退回我方注销。\n" +
                "本保函项下所有权利和义务均受中华人民共和国法律管辖和制约。";
    }

}
